#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;

struct A {
    A() { cout << "A()" << endl; }
    ~A() { cout << "~A()" << endl; }
};

struct B {
    boost::weak_ptr<A> Parent;

    B() { cout << "B()" << endl; }
    ~B() { cout << "~B()" << endl; }
};

int main() {
    A* p = new A();
    const A* p1 = p;
    boost::shared_ptr<const A> a(p);

    cout << "Test1" << endl;
    {
        boost::shared_ptr<B> b(new B());
        boost::shared_ptr<A> a(new A());
        b->Parent = a;

        cout << b->Parent.lock() << endl;
        boost::shared_ptr<A> a1(b->Parent);
    }
    cout << endl;

    cout << "Test2" << endl;
    {
        boost::shared_ptr<B> b(new B());
        {
            boost::shared_ptr<A> a(new A());
            b->Parent = a;
        }

        cout << b->Parent.lock() << endl;
        boost::shared_ptr<A> a1(b->Parent);
    }
    cout << endl;

    return 0;
}
